using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ninject;
using Tetris.Interfaces;
using Tetris.Screens;

namespace Tetris
{
    public class GameManager : Game, IGameManager
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _mainSpriteBatch;
        private IKernel _kernel;
        private IScreenManager _screenManager;
        private GameTime _gameTime;

        public IKernel ServiceLocator { get { return _kernel; } }

        public Game Game
        {
            get { return this; }
        }

        public GameTime GameTime { get { return _gameTime; } }

        public GameManager()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            _mainSpriteBatch = new SpriteBatch(GraphicsDevice);
            _kernel = Bootstrapper.Bootstrap(this);

            _screenManager = _kernel.Get<IScreenManager>();
            _screenManager.AddScreen(this, new BackgroundScreen(), null);
            _screenManager.AddScreen(this, new MainMenuScreen(this), null);

            _screenManager.Initialize(this);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            _mainSpriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            _gameTime = gameTime;
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here
            _screenManager.Update(this);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            _gameTime = gameTime;
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _screenManager.Draw(_mainSpriteBatch, this, gameTime);
            base.Draw(gameTime);
        }
    }
}
